#! /bin/bash
# This file should be saved as /etc/init.d/mongodb
# Copyright 2011 Flavio Codeco Coelho <fccoelho at gmail dot com>
#
### BEGIN INIT INFO
# Provides:          mongodb
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the mongodb sharding node
# Description:       starts mongodb sharding node using start-stop-daemon
### END INIT INFO
source /etc/mongosharding.conf

PATH          = /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SHARDPIDFILE  = /usr/local/mongodb/logs/shard.pid
CONFIGPIDFILE = /usr/local/mongodb/logs/configdb.pid
MONGOSPIDFILE = /usr/local/mongodb/logs/mongos.pid
SHARDLOGFILE  = /usr/local/mongodb/logs/shard.log
CONFIGLOGFILE = /usr/local/mongodb/logs/configdb.log
MONGOSLOGFILE = /usr/local/mongodb/logs/mongos.log
SHARD         = $(which mongod)
SHARD_ARGS    = "--fork --shardsvr --dbpath $DATAPATH --port $SHARDPORT --logpath $SHARDLOGFILE"
CONFIG        = $(which mongod)
CONFIG_ARGS   = "--fork --configsvr --dbpath $CONFIGDATAPATH --port $CONFIGPORT --logpath $CONFIGLOGFILE"
MONGOS        = $(which mongos)
MONGOS_ARGS   = "--fork --configdb $CONFIG_SERVER:$CONFIGPORT --logpath $MONGOSLOGFILE"

NAME = mongodb
DESC = mongodb

test -x $SHARD || exit 0
test -x $CONFIG || exit 0
test -x $MONGOS || exit 0

#set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon -m --pidfile $SHARDPIDFILE --exec $SHARD --start -- $SHARD_ARGS >> $SHARDLOGFILE&
        if $HOST_CONFIG;
            then
            echo -n "Starting MongoDB Config daemon"
            start-stop-daemon -m --pidfile $CONFIGPIDFILE --exec $CONFIG --start -- $CONFIG_ARGS >> $CONFIGLOGFILE&
            echo -n "Waiting for Config daemon to start:"
            while [ ! -e $CONFIGPIDFILE ] 
                do
                   sleep 1 #waits for config to start
                   echo -n "."
                done
        fi
        echo -n "Starting MongoDB Mongos daemon"
        start-stop-daemon -m --pidfile $MONGOSPIDFILE --exec $MONGOS --start -- $MONGOS_ARGS >> $MONGOSLOGFILE&
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --quiet --pidfile $SHARDPIDFILE --exec $SHARD --stop --retry=TERM/10/KILL/3
        start-stop-daemon --quiet --pidfile $CONFIGPIDFILE --exec $CONFIG --stop --retry=TERM/10/KILL/3
        start-stop-daemon --quiet --pidfile $MONGOSPIDFILE --exec $MONGOS --stop --retry=TERM/10/KILL/3
        echo "$NAME."
        killall mongod # making sure they stop 
        killall mongos
        sudo rm /usr/local/mongodb/logs/*.pid
        ;;
  restart|force-reload)
        echo "Restarting $DESC: "
        start-stop-daemon --quiet --pidfile $SHARDPIDFILE --exec $SHARD --stop --retry 3
        sleep 1
        start-stop-daemon -m --pidfile $SHARDPIDFILE --exec $SHARD --start -- $SHARD_ARGS >> $SHARDLOGFILE&
        if $HOST_CONFIG;
        then
            start-stop-daemon --quiet --pidfile $CONFIGPIDFILE --exec $CONFIG --stop --retry 3
            sleep 1
            start-stop-daemon -m --pidfile $CONFIGPIDFILE --exec $CONFIG --start -- $CONFIG_ARGS >> $CONFIGLOGFILE&
            echo -n "Waiting for Config daemon to start:"
            while [ ! -e $CONFIGPIDFILE ]
                do
                   sleep 1 #waits for config to start
                   echo -n "."
                done
        fi
        start-stop-daemon --quiet --pidfile $MONGOSPIDFILE --exec $MONGOS --stop --retry 3
        sleep 1
        echo -n "Starting MongoDB Mongos daemon"
        start-stop-daemon -m --pidfile $MONGOSPIDFILE --exec $MONGOS --start -- $MONGOS_ARGS >> $MONGOSLOGFILE&
        echo "$NAME."
        ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile $SHARDPIDFILE --exec $SHARD
      echo "$NAME."
      ;;
      *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0